package com.sitech.portal.server.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sitech.portal.server.dao.IMessageCenterDao;
import com.sitech.portal.server.domain.Message;
import com.sitech.portal.server.domain.MessageSetting;
import com.sitech.portal.server.form.MessageSettingForm;
import com.sitech.portal.server.service.IMessageCenterService;
import com.sitech.portal.server.service.IMessageCollectorService;
import com.sitech.portal.system.domain.TblSYS_LOGINMSG;

public class MessageCenterServiceImpl implements IMessageCenterService {

	private Log log = LogFactory.getLog(MessageCenterServiceImpl.class);
    private IMessageCollectorService messageCollectorService;
    private IMessageCenterDao messageCenterDao;
    private String afterTime;
	
    public IMessageCollectorService getMessageCollectorService() {
		return messageCollectorService;
	}

	public void setMessageCollectorService(
			IMessageCollectorService messageCollectorService) {
		this.messageCollectorService = messageCollectorService;
	}
	
	public String getAfterTime() {
    	Calendar cal = Calendar.getInstance();
        afterTime = new SimpleDateFormat(messageCollectorService.JAVA_TIME_FORMAT).format(cal.getTime());
        return afterTime;
    }

    public void setAfterTime(String afterTime) {
        this.afterTime = afterTime;
    }
	
	public IMessageCenterDao getMessageCenterDao() {
		return messageCenterDao;
	}

	public void setMessageCenterDao(IMessageCenterDao messageCenterDao) {
		this.messageCenterDao = messageCenterDao;
	}

	@Override
	public List<Message> findUnreadMessage(String login_id, String msg_id,MessageSettingForm settingForm) {
		MessageSetting setting = new MessageSetting(settingForm);
		List<Message> messages = messageCenterDao.findUnreadMessage(login_id, msg_id,setting);
		updateDisplaySum(messages);
		return messages;
	}

	@Override
	public Message insertMessage(Message msg) {
		// TODO Auto-generated method stub
		return messageCenterDao.insertMessage(msg);
	}

	@Override
	public void readMessage(String msg_id, String login_id) {
		String msgIds[] = msg_id.split(",");
		for(String id : msgIds){
			messageCenterDao.readMessage(id, login_id);
		}
	}
	
	/**
     * 保存消息
     */
    public void saveMessages() {
        log.debug("传入时间：" + this.getAfterTime());
        List<Message> list = messageCollectorService.loadNewMessage(this.getAfterTime());
        log.debug("保存Message\n" + list);
        for (int i = 0; i < list.size(); i++) {
            Message msg = list.get(i);
            insertMessage(msg);
        }
        if (list.size() > 0) {
            String time1 = list.get(0).getSEND_TIME();
            String time2 = list.get(list.size() - 1).getSEND_TIME();
            this.setAfterTime(time1.compareTo(time2) < 0 ? time2 : time1);
            log.debug("更新afterTime:" + this.getAfterTime());
        }
    }
    /**
     * 加载信息
     */
	@Override
	public List<Message> loadMsgByResponseId(String responseId) {
		return messageCenterDao.loadMsgByResponseId(responseId);
	}

	@Override
	public void saveMsg(Message msg) {
		//msg.setMSG_ID(String.valueOf(System.currentTimeMillis()));
		messageCenterDao.saveMsg(msg);
		messageCenterDao.saveMsgUser(msg);
	}

	@Override
	public List<Message> loadOnlineServerMsg(Message msg,MessageSettingForm settingForm) {
		MessageSetting setting = new MessageSetting(settingForm);
		List<Message> messages = messageCenterDao.loadOnlineServerMsg(msg,setting);
		//updateDisplaySum(messages);
		return messages;
	}

	@Override
	public List<TblSYS_LOGINMSG> loadOnlineMsgUser(String loginId,MessageSettingForm settingForm) {
		//MessageSetting setting = new MessageSetting(settingForm);
		return messageCenterDao.loadOnlineMsgUser(loginId,null);
	}

	@Override
	public boolean updateDisplaySum(List<Message> messages) {
		boolean flag = true;
		for(Message message : messages){
			boolean upFlag = messageCenterDao.updateDisplaySum(message);
			if(!upFlag){
				flag = false;
			}
		}
		return flag;
	}
	
}
